//
//  MBLoginInputTableViewCell.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2017/12/27.
//  Copyright © 2017年 zhangXin. All rights reserved.
//

#import "MBLoginInputTableViewCell.h"

@interface MBLoginInputTableViewCell ()
@property (nonatomic,weak) UIImageView* iconImageView;
@property (nonatomic,weak)UIButton* rightButton;

@end

@implementation MBLoginInputTableViewCell
@dynamic viewModel;

-(void)setupLayout
{
    [super setupLayout];
    
    UIView* contantView = [[UIView alloc]init];
    contantView.layer.borderColor = [UIColor colorWithR:221 G:221 B:221].CGColor;
    contantView.layer.cornerRadius = [UIView lf_sizeFromIphone6:4];
    contantView.layer.masksToBounds = YES;
    contantView.layer.borderWidth = [UIView lf_sizeFromIphone6:1];
    [self.contentView addSubview:contantView];
    
    [contantView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, [UIView lf_sizeFromIphone6:24], [UIView lf_sizeFromIphone6:12], [UIView lf_sizeFromIphone6:24]));
    }];
    
    UITextField* inputTextField = [[UITextField alloc]init];
    self.inputTextField = inputTextField;
    inputTextField.placeholder = @"请输入手机号";
    inputTextField.font = [UIFont lf_systemFontOfSize:15];
    inputTextField.tintColor = COLOR_MAIN;
    inputTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [contantView addSubview:inputTextField];
    
    [inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contantView);
        make.right.equalTo(contantView).offset([UIView lf_sizeFromIphone6:-18]);         make.top.bottom.equalTo(contantView);
    }];
    
    UIImageView* iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIView lf_sizeFromIphone6:49], [UIView lf_sizeFromIphone6:22])];
    self.iconImageView = iconImageView;
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    inputTextField.leftViewMode = UITextFieldViewModeAlways;
    inputTextField.leftView = iconImageView;
    
    UIButton* rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, [UIView lf_sizeFromIphone6:25], [UIView lf_sizeFromIphone6:30])];
    [rightButton setImage:[UIImage imageNamed:@"user_delete"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(handleClickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = rightButton;
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    inputTextField.rightView = rightButton;
    inputTextField.rightViewMode = UITextFieldViewModeAlways;

}

-(void)handleClickRightButton:(UIButton*)button
{
    if ([self.viewModel.dataModel.placeHolder containsString:@"密码"]) {
        button.selected = !button.selected;
        self.inputTextField.secureTextEntry = button.selected;
    }else {
        self.inputTextField.text = @"";
    }
}

-(void)setupData
{
    [super setupData];
    
    self.iconImageView.image = [UIImage imageNamed:self.viewModel.dataModel.title];
    self.inputTextField.placeholder = self.viewModel.dataModel.placeHolder;
    self.inputTextField.text = self.viewModel.dataModel.text;
    self.inputTextField.keyboardType = self.viewModel.dataModel.keyboardType;
    [self.rightButton setImage:[UIImage imageNamed:self.viewModel.dataModel.rightIcon] forState:UIControlStateNormal];
    
    if ([self.viewModel.dataModel.rightIcon containsString:@"kejian_nor"]) {
        [self.rightButton setImage:[UIImage imageNamed:@"kejian_nor"] forState:UIControlStateSelected];
        [self.rightButton setImage:[UIImage imageNamed:@"login_kejian_pre"] forState:UIControlStateNormal];
        self.rightButton.selected = YES;
        self.inputTextField.secureTextEntry = YES;
    }
}

// 用来计算高度
+(NSNumber*)calculateRowHeightWithViewModel:(MBLoginTableCellViewModel *)viewModel
{
    return [viewModel cacheCellHeightWithCalculateBlock:^NSNumber * _Nonnull{
        return viewModel.totalHeight;
    }];
}

@end
